﻿import com.dynamicflash.utils.Delegate;
import com.kelvinluck.flickr.Flickr;
import com.kelvinluck.flickr.FlickrResponseListener;
import com.kelvinluck.flickr.Person;
import com.kelvinluck.flickr.Photo;
import com.kelvinluck.util.LogWrapper;

import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.filters.BitmapFilter;
import mx.transitions.Tween;
import mx.transitions.easing.Regular;

class com.mentalaxis.flickr.polkr.Polkr {
	/**
	 * Your API Key.
	 * 
	 * @see <http://www.flickr.com/services/api/misc.api_keys.html>
	 */
	var apiKey : String = "3de9860c2e53558a1e9ea569805e63c5";
	/**
	 * The NSID of the user whose photos you want to get
	 */
	var userNsid : String = "75556562@N00";
	/**
	 * The main MovieClip
	 */
	var canvas : MovieClip;
	/**
	 * The photo holder
	 */
	var photoClip : MovieClip;
	/**
	 * The polkr construction space
	 */
	var polkrClip : MovieClip;
	/**
	 * bitmap data
	 */
	var bmData : BitmapData;
	/**
	 * Top x margin
	 */
	var topX : Number = 20;
	/**
	 * Top y margin
	 */
	var topY : Number = 20;
	/**
	 * image size multiplier
	 */
	var multiplex : Number = 7;
	/**
	 * image size
	 */
	var square : Number = 75;
	/**
	 * dot size
	 */
	var dotSize : Number = 1;
	/**
	 * knocked back alpha
	 */
	var alphaShadow : Number = 0x40000000;
	/**
	 * alpha size
	 */
	var shadowSize : Number = 5;
	/**
	 * Flickr person object
	 */
	var person : Person;
	/**
	 * photo return limit
	 */
	var limit : Number = 500;
	/**
	 * Constructor
	 */
	var index : Number = 0;
	function Polkr(target:MovieClip) { // target is _root here
		Stage.scaleMode = "noScale";
		Stage.align = "TL";
		
		System.security.allowDomain("*");
		
		trace("Polkr 1.0 - mentalaxis.com");		
		trace("Dimensions ");		
		trace(": "+ ((square * multiplex) + (topX*2)));		
		
		// initalise logging to Flash's output window
		LogWrapper.getInstance().init();
		LogWrapper.getInstance().addTracePublisher();

		canvas = target;
		photoClip = canvas.createEmptyMovieClip("photoClip", 100);
		var _flickr:Flickr = Flickr.getFlickr();
		_flickr.apiKey = apiKey;
		var _flickrResponseListener:FlickrResponseListener = new FlickrResponseListener();
		_flickrResponseListener.setSuppressOutput(true);
		_flickrResponseListener.onPeopleGetPublicPhotos = Delegate.create(this, onPeopleGetPublicPhotos);
		_flickr.peopleGetPublicPhotos(userNsid, null, limit);
	} // end constructor

	/**
	* Function: onPeopleGetPublicPhotos
	* Called when the _flickrResponseListener hears a response to flikr.people.getPublicPhotos
	*
	* Parameters:
	* p - The <Person> whose Photos you just got.
	**/
	function onPeopleGetPublicPhotos(p:Person):Void {
		person = p;
		polkrMaker();
	}

	function polkrMaker():Void {
		var photo:Photo = person.getPhotos()[index];
		index = (index < limit) ? index + 1 : 0;
		trace (photo.thumbnailUrl);
		var loader:MovieClipLoader = new MovieClipLoader();
		var listener:Object = new Object();
		listener.onLoadInit = Delegate.create(this, photoLoaded);
		loader.addListener(listener);
		bmData = new BitmapData(square, square, false, 0);
		photoClip._alpha = 0;
		loader.loadClip(photo.thumbnailUrl, photoClip);
		// loader.loadClip("proxy.php?url=" + photo.thumbnailUrl, photoClip);
		_root.titletext._alpha = 0;
		_root.titletext.title.text = index + " : " + photo.title.toUpperCase();
	}

	function photoPause():Void {
		var a:Object = new Object();
		a.time = 0;
		var t:Tween = new Tween(a, "time", Regular.easeIn, 0, 100, .1, true);
		t["onMotionFinished"] = Delegate.create(this, photoLoaded);
	}

	function photoLoaded():Void {
		photoClip.alpha = 100;
		bmData.draw(photoClip);
		photoClip._alpha = 0;
		polkrClip = canvas.createEmptyMovieClip("polkrClip", 200);
		polkrClip._alpha = 0;
		polkrClip.time = 0;
		var size : Number = multiplex * square;
		var bmp : BitmapData = new BitmapData(size, size, true, 0);
		polkrClip.attachBitmap(bmp, 1);
		polkrClip._x = topX;
		polkrClip._y = topY;
		for(var y:Number = 0; y<square; y++){
			for(var x:Number = 0; x<square; x++){
				var baseX : Number = x*multiplex;
				var baseY : Number = y*multiplex;
				bmp.fillRect(new Rectangle(baseX, baseY, shadowSize, shadowSize), bmData.getPixel(x, y) + alphaShadow);
				bmp.fillRect(new Rectangle(baseX+(shadowSize/2), baseY+(shadowSize/2), dotSize, dotSize), bmData.getPixel32(x, y));
			}
		}
		var waitTween : Tween = new Tween(polkrClip, "_alpha", Regular.easeOut, 0, 100, 3, true);
		new Tween(_root.titletext, "_alpha", Regular.easeOut, 0, 100, 3, true);
		waitTween["onMotionFinished"] = Delegate.create(this, waitFade);
	}

	private function waitFade():Void {
		var waitTween : Tween = new Tween(polkrClip, "time", Regular.easeOut, 100, 0, 5, true);
		new Tween(_root.titletext, "time", Regular.easeOut, 100, 0, 5, true);
		waitTween["onMotionFinished"] = Delegate.create(this, doneWait);		
	}
	
	private function doneWait():Void {
		var waitTween : Tween = new Tween(polkrClip, "_alpha", Regular.easeOut, 100, 0, 3, true);
		new Tween(_root.titletext, "_alpha", Regular.easeOut, 100, 0, 3, true);
		waitTween["onMotionFinished"] = Delegate.create(this, polkrMaker);		
	}
	
	/**
	 * Utility function to convert a number to a 
	 * corresponding hex color string.
	 */
	private function color2Hex32(color:Number, alphaHex:String):Number {
		var colorStr:String = color.toString(16);
		var l : Number = colorStr.length;
		for (var i: Number = 0; i < 6 - l; i++){
			colorStr = "0" + colorStr;					
		}
		var hexStr : String = "0x" + alphaHex + colorStr;
		trace(hexStr);
		
		return Number(hexStr);
	}

	/**
	* Function: main
	* Entrypoint to the application.
	**/
	public static function main(target:MovieClip):Void{
		trace("INFO: com.mentalaxis.flickr.polkr.Polkr.main()");
		var polkr:Polkr = new Polkr(target);
	} // End main

	/**
	* Function: toString
	**/
	public function toString():String {
		return "[com.mentalaxis.flickr.polkr.Polkr]";
	} // end toString
}